package thread;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2023-11-26  21:29
 */
public class ThreadDemo15 {
    private static long result = 0;
    private static long odd = 0;
    private static long even = 0;
    public static void main(String[] args) throws InterruptedException {
        int[] array = new int[1000_0000];
        Random random = new Random();

        for(int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100) + 1;
        }

        Thread thread1 = new Thread(() -> {
            for(int i = 0; i < array.length; i+=1) {
                odd += i;
            }
        });
        Thread thread2 = new Thread(() -> {
            for(int j = 0; j < array.length; j+=2) {
                even += j;
            }
        });
        thread1.start();
        thread2.start();

        long beg = System.currentTimeMillis();

        thread1.join();
        thread2.join();

        long end = System.currentTimeMillis();

        System.out.println("result = " + (odd + even));
        System.out.println("time = " + (end-beg) + "ms");
    }
}
